package com.huoniu.api.controller;

import com.huoniu.api.entity.Resource;
import com.huoniu.api.entity.User;
import com.huoniu.api.service.ResourceService;
import com.huoniu.api.service.UserService;
import com.huoniu.common.core.Result;
import com.huoniu.common.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.huoniu.api.constrant.GlobalConstrant.TOKEN_PASSWORD;

/**
 * @类 名： ResourceController <br/>
 * @描 述： <br/>
 * @日 期： 2020/9/11 10:11<br/>
 * @作 者： 侯建军<br/>
 * @版 本： 1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/resource")
@Slf4j
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserService userService;

    @GetMapping("/menu")
    public Result getMenu(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<Resource> resources = null;
        if (!StringUtils.isEmpty(token)) {
            String username = JwtUtils.getUsername(token);
            User user = userService.getUserByUsername(username);
            resources = resourceService.getMenuTree(user.getRoleId());
        }
        return Result.success(resources);
    }

    @GetMapping("/all")
    public Result getMenuAll() {
        List<Resource>  resources = resourceService.getMenuTree(null);
        return Result.success(resources);
    }
}
